﻿using ConfExpert.Forms.Base;

using DevExpress.XtraReports.UI;
using DevTeam;

using System;
namespace ConfExpert.Forms.Depo.Cikis
{
    public partial class KumasCikisIrsaliye : XForm
    {
        public KumasCikisIrsaliye()
        {
            InitializeComponent();
        }

        private void KumasCikisIrsaliye_Load(object sender, EventArgs e)
        {
            de.Hint = "Başlangıç Tarihi";
            de2.Hint = "Bitiş Tarihi";

            de.EditValue = DateTime.Now.AddMonths(-1);

            gvIrsaliye.Init("GetAllKumasIrsaliye", 0, 0,
                de.EditValue.ToDate() == new DateTime(2000, 1, 1) ? new DateTime(2001, 1, 1) : de.EditValue.ToDate(),
                de2.EditValue.ToDate() == new DateTime(2000, 1, 1) ? new DateTime(2071, 1, 1) : de2.EditValue.ToDate()
                );
            gvIrsaliye.AddDetailGrid(gvArticles, "KumasIrsaliyeId");
            gvArticles.AddDetailGrid(gvRolls, "KumasIrsaliyeId,ProductId");

            gvIrsaliye.MakeReadOnly();
            gvIrsaliye.MakeColumnInvisible("KumasIrsaliyeId");
            gvArticles.MakeColumnInvisible("ProductId");
            gvRolls.MakeColumnInvisible("Scan");


            gvIrsaliye.MakeRowSelectable();
            gvIrsaliye.FormatAsNumber(1, "Metraj");
            gvIrsaliye.AddSummary("Metraj");
            gvArticles.AddSummary("Metraj");
            gvRolls.AddSummary("Qty");

            gvIrsaliye.Columns["IrsaliyeTarihi"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            gvIrsaliye.Columns["IrsaliyeTarihi"].DisplayFormat.FormatString = "d"; //G

            BestFitForThisForm();

            GridManager gm = GridManager.GetManager(gvIrsaliye);
            gm.GridMenu.AddMenu("Bu irsaliyeyi yeniden yazdır", RePrintShip);
            gm.GridMenu.AddMenu("Çeki Listesi", PrintCeki);


            gleProduct.Edit.Assign(RI(Rp.V_Kumas));
            txtRoll.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

            cmbTip.EditValueChanged += cmbTip_EditValueChanged;
            btnGuncelle.ItemClick += btnGuncelle_ItemClick;
            //btnSil.Click += new EventHandler(btnSil_Click);

        }

        private void BestFitForThisForm()
        {
            gvIrsaliye.BestFitColumns();
            gvArticles.BestFitColumns();
            gvRolls.BestFitColumns();

            gvIrsaliye.Columns["Notlar"].Width = 150;
            gvIrsaliye.Columns["CikisEmirleri"].Width = 100;
            gvIrsaliye.Columns["ParcaCikisEmirleri"].Width = 100;
        }

        private void PrintCeki(object sender, EventArgs e)
        {
            int IrsaliyeId = intParse(gvIrsaliye.GetFocusedRowCellValue("KumasIrsaliyeId"));
            using (Rapor.xrIrsaliye irsDetay = new Rapor.xrIrsaliye(IrsaliyeId, 0))
                irsDetay.ShowPreviewDialog();
        }

        void btnSil_Click(object sender, EventArgs e)
        {
            gleProduct.EditValue = null;
            //txtRoll.Text = "";
        }

        void cmbTip_EditValueChanged(object sender, EventArgs e)
        {
            gleProduct.EditValue =
            txtRoll.EditValue = null;

            switch (cmbTip.EditValue.ToStr())
            {
                case "Kumaş":
                    gleProduct.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    txtRoll.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    break;

                case "Top":
                    gleProduct.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    txtRoll.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    break;
            }
        }

        void RePrintShip(object sender, EventArgs e)
        {
            int IrsaliyeId = intParse(gvIrsaliye.GetFocusedRowCellValue("KumasIrsaliyeId"));
            using (KumasIrsaliyeOlustur ki = new KumasIrsaliyeOlustur(IrsaliyeId))
                ki.ShowDialog();
        }

        void btnGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int selIndex = -1;
            int Id = 0;
            switch (cmbTip.EditValue.ToStr())
            {
                case "Kumaş":
                    selIndex = 0;
                    Id = gleProduct.EditValue.ToInt();
                    break;

                case "Top":
                    selIndex = 1;
                    Id = txtRoll.EditValue.ToInt();
                    break;
            }

            gvIrsaliye.RefreshDataSource("GetAllKumasIrsaliye", selIndex, Id,
                de.EditValue.ToDate() == new DateTime(2000, 1, 1) ? new DateTime(2001, 1, 1) : de.EditValue.ToDate(),
                de2.EditValue.ToDate() == new DateTime(2000, 1, 1) ? new DateTime(2071, 1, 1) : de2.EditValue.ToDate()
                );
            BestFitForThisForm();
        }

    }
}